Passed
Pull Request — master (#2)
by André
01:32
created

out.js ➔ ???   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 33
c 6
b 0
f 0
nc 2
dl 0
loc 52
rs 9.0879
nop 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A out.js ➔ ... ➔ ??? 0 4 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
'use strict'
2
3
module.exports = (input, callback) => {
4
	const mqtt = require('mqtt')
5
	const configuration = require('./configuration')
6
	const validate = require('./validate')
7
	let error = null
8
	let output = null
9
10
	validate.sourceConfiguration(input, (validatedInput, thrownError) => {
11
		input = validatedInput
12
		error = thrownError
13
	})
14
	validate.paramConfiguration(input, (validatedInput, thrownError) => {
15
		input = validatedInput
16
		error = thrownError
17
	})
18
19
	// Send MQTT
20
	if ( !error ) {
21
		let configurationMqtt = configuration.mqtt(input)
22
		let client = mqtt.connect(input.source.url, configurationMqtt)
23
24
		client.on('connect', () => {
25
			client.subscribe(input.source.topic, (errorConnection) => {
26
				if ( !errorConnection ) {
27
					client.publish(input.source.topic, input.params.payload, {
28
						qos: input.params.qos,
29
						retain: false
30
					})
31
				} else {
32
					error = errorConnection
33
				}
34
			})
35
		})
36
37
		client.on('message', (topic, message) => {
0 ignored issues
show
Unused Code introduced by
The parameter message is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter topic is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
38
			client.end()
39
		})
40
41
		output = {
42
			'version': {'ref': 'output'},
43
			'metadata': [
44
				{'name': 'message', 'value': input.params.payload.toString()},
45
				{'name': 'qos', 'value': input.params.qos.toString()},
46
				{'name': 'timestamp', 'value': Date.now().toString()},
47
				{'name': 'topic', 'value': input.source.topic}
48
			]
49
		}
50
	}
51
52
	callback(error, output)
53
54
}
55